package com.n5x.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.n5x.common.base.JsonModel;
import com.n5x.common.base.ResponseCode;
import com.n5x.common.upload.FileUpload;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.VelocityConfigurerBeanDefinitionParser;


import net.sf.json.JSONObject;

/**
 * 后台用户-controller
 * @author libo
 */
@Controller
@RequestMapping("/")
public class FileUploadController extends  BaseController {

    @Value(value = "${imgPath}")    //后台图片保存地址
    private String imgPath;


    /**
     * 上传用户头像
     */
    @ResponseBody
    @RequestMapping(value = "uploadImg", method = RequestMethod.POST)
    public JsonModel uploadSysHeadImg(@RequestParam(value = "fileArray") MultipartFile[] fileArray) {
        Map jo = new HashMap();

        try {

            List<String> fileNameList = new ArrayList();
            List<String> relaPathList = new ArrayList();
            JsonModel jsonModel = new JsonModel();
            for (MultipartFile pic : fileArray) {
                if (!checkFile(pic.getOriginalFilename())) {

                    return getResultError(ResponseCode.Common_Error, "文件類型錯誤");
                }
                String uploadInfo = FileUpload.upload(pic, request, response, imgPath);
                if (!"".equals(uploadInfo)) {    //上传成功
                    String[] infoList = uploadInfo.split(";");
                    fileNameList.add(infoList[0]);    //文件名
                    relaPathList.add(infoList[1]);    //相对路径
                } else {    //上传失败
                    fileNameList.add("");
                    relaPathList.add("");
                }
            }
            jo.put("success", 1);
            jo.put("error", null);
            jo.put("fileNameList", fileNameList);
            jo.put("relaPathList", relaPathList);
        } catch (Exception e) {
            jo.put("success", 0);
            jo.put("error", "上传失败");
        }
        return getResult(jo);
//        ResponseUtils.renderJson(response, jo.toString());
    }


    /**
     * 判断是否为允许的上传文件类型,true表示允许
     */
    private boolean checkFile(String fileName) {
        //设置允许上传文件类型
        String suffixList = "jpg,gif,png,ico,bmp,jpeg";
        // 获取文件后缀
        String suffix = fileName.substring(fileName.lastIndexOf(".")
                + 1, fileName.length());
        if (suffixList.contains(suffix.trim().toLowerCase())) {
            return true;
        }
        return false;
    }

}